#include "DisholUnicode.h"

#include <windows.h>

namespace Dishol
{
/*
std::string	Unicode::TCHARToUTF8( const TCHAR* tcharString )
{
#ifdef _UNICODE
	return UTF16toUTF8String( tcharString );
#else
	return UTF16toUTF8String( MBCStoUTF16String( tcharString ) );
#endif
}*/

std::wstring Unicode::MBCStoUTF16String( const std::string& mbcsString )
{
	if( mbcsString.empty() )    
		return std::wstring();
	// ACP means ANSI Code Page. The conversion depends on the computer current code page settings.
	// See http://msdn.microsoft.com/en-us/library/dd319072%28VS.85%29.aspx
	size_t retStringSize = ::MultiByteToWideChar( CP_ACP, 0, mbcsString.c_str(), mbcsString.length(), 0, 0 );
    std::wstring retString( retStringSize, L'\0' );
    ::MultiByteToWideChar( CP_UTF8, 0, mbcsString.c_str(), mbcsString.length(), &retString[0], retString.length() );
    return retString;
}

// Use this method when the program is compiled with UNICODE defined (aka UTF16)
std::string Unicode::UTF16toUTF8String( const std::wstring& utf16String )
{
	if ( utf16String.empty() )
		return std::string();
	int retStringSizeInByte = ::WideCharToMultiByte( CP_UTF8, 0, utf16String.c_str(), utf16String.length(), 0, 0, 0, 0 );
	std::string retString( retStringSizeInByte, '\0' );
	::WideCharToMultiByte( CP_UTF8, 0, utf16String.c_str(), utf16String.length(), &retString[0], retStringSizeInByte, 0 ,0 );
	return retString;	
}

std::wstring Unicode::UTF8toUTF16String( const std::string& utf8String )
{
	if( utf8String.empty() )    
		return std::wstring();
	size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, utf8String.c_str(), utf8String.length(), 0, 0 );
    std::wstring ret( reqLength, L'\0' );
    ::MultiByteToWideChar( CP_UTF8, 0, utf8String.c_str(), utf8String.length(), &ret[0], ret.length() );
    return ret;
}

}